package com.softserve.academy.food.services.admin;


import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.softserve.academy.food.dao.HibernateUser;
import com.softserve.academy.food.entity.eUser;
import com.softserve.academy.food.model.mUserCredentials;
import com.softserve.academy.food.model.mUserInfo;
import com.softserve.academy.food.services.supplement.Convector;

@Service
public class UserManagerService implements iUserManagerService
{
	@Autowired
	private HibernateUser daoUser;

	@Override
	@Transactional
	public ArrayList<mUserCredentials>  getUsers()
	{
		ArrayList<mUserCredentials> list = new ArrayList<mUserCredentials>();
	
		ArrayList<eUser> elist = daoUser.ReadAll();
		
		for (eUser user : elist)
		{
			list.add( new mUserCredentials( user ) );
		}
		
		return list;
	}

	@Override
	@Transactional
	public mUserInfo getUserInfo(int id)
	{
		return new mUserInfo( daoUser.Read( id ) );
	}
	
	@Override
	@Transactional
	public mUserCredentials getUser(int id)
	{
		return new mUserCredentials( daoUser.Read( id ) );
	}
	
	@Override
	@Transactional
	public ArrayList<mUserCredentials> findUsers( String login )
	{
		ArrayList<mUserCredentials> findlist = new ArrayList<mUserCredentials>();
		
		if (login!=null)
		{
			ArrayList<eUser> elist = daoUser.findUser( login );

			for (eUser user : elist)
			{
				findlist.add( new mUserCredentials( user ) );
			}
		}
		
		return findlist;
	}
	
	@Override
	@Transactional
	public void delUser(String id)
	{
		daoUser.Delete( Convector.StringToInt(id) );
	}
	
	
}
